package com.code.app.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.support.annotation.Nullable;

import com.code.app.entities.DownLoadInfo;
import com.code.app.util.NotificationUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DownLoaderService extends Service {

    private final String TAG = "DownLoaderService";

    public static final String TASK_START = "com.code.app.Service.Start";

    public static final String TASK_STOP = "com.code.app.Service.Stop";

    public static final int TASK_BIND = 0x001;

    public static final int TASK_ADD = 0x002;

    public static final int TASK_PROCESS = 0x003;

    private Random random;

    private List<DownLoadInfo> downLoadInfoList;

    private Messenger activityMessenget;

    private NotificationUtil notificationUtil;

    private DownLoaderHandler handler;

    class DownLoaderHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case TASK_BIND:
                    activityMessenget = msg.replyTo;
                    break;
                case TASK_ADD:
                    DownLoadInfo downLoadInfo = (DownLoadInfo) msg.obj;
                    downLoadInfoList.add(downLoadInfo);
                    notificationUtil.showNotification(downLoadInfo);

                    handler.sendEmptyMessage(TASK_PROCESS) ;
                    break;
                case TASK_PROCESS:
                    handleProgress();
                    if (handler.hasMessages(TASK_PROCESS)) {
                        handler.removeMessages(TASK_PROCESS);
                    }
                    handler.sendEmptyMessageAtTime(TASK_PROCESS, 1000);
                    break;
            }
        }
    }

    private void handleProgress() {
        synchronized (downLoadInfoList){
            for (DownLoadInfo downLoadInfo : downLoadInfoList) {
                downLoadInfo.setProgress(5 + downLoadInfo.getProgress());
                if (downLoadInfo.getProgress() >= 100) {
                    notificationUtil.cancleNotification(downLoadInfo.getId());
                    downLoadInfoList.remove(downLoadInfo);
                } else {
                    notificationUtil.updateProgress(downLoadInfo.getId(), downLoadInfo.getProgress());
                }

            }
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action = intent.getAction();
        if (TASK_START.equals(action)) {

        } else if (TASK_STOP.equals(action)) {

        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        if (handler == null) {
            handler = new DownLoaderHandler();
        }
        if (downLoadInfoList == null) {
            downLoadInfoList = new ArrayList<>();
        }
        if (random == null) {
            random = new Random();
        }
        if (notificationUtil == null) {
            notificationUtil = new NotificationUtil(this);
        }
        super.onCreate();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Messenger messenger = new Messenger(handler);
        return messenger.getBinder();
    }
}
